/*
 * to change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javamail.web;

import java.util.Calendar;
import java.util.Properties;
import javamail.speech.speechSynthesizer;
import javamail.common.stringEncoder;

/**
 *
 * @author sriram
 */
public class sssParser {
    String toParse = null;
    boolean parsed = false;
    Properties parms = new Properties();
    
    public sssParser(String parse) {
        toParse = new String(parse);
    }
    
    public void parse() {
        int count = 0;
        if (toParse != null) {
            String[] element = toParse.split("&");
            String[] f = null;
            for(int i=0; i<element.length; i++) {
                f = element[i].split("=");
                count = f.length;
                if(count >= 2) {
                    parms.setProperty(f[0], f[1]);
                }
            }
        }
    }
    public String getProperty(String name) {
        return parms.getProperty(name);
    }
    
    public String processInput(String fileName) {
        if(fileName.toLowerCase().trim().equals("query.sss"))
            return httpGenerator.queryPage();
        else {
            String delete = parms.getProperty("delete");
            String update = parms.getProperty("update");
            if(delete!=null && !delete.trim().equals("")) {
                javamail.main.glblConfig.deleteMessageText();
                return httpGenerator.successResponse("Message successfully deleted");
            } else if(update!=null && !update.trim().equals("")) {
                String newMsg = parms.getProperty("new");
                if(newMsg!=null && !newMsg.trim().equals("")) {
                    String text = stringEncoder.urlToText(newMsg);
                    if(text!=null) {
                        javamail.main.glblConfig.setMessageText(text);
                        javamail.main.glblConfig.setMessageReceived(Calendar.getInstance().getTime());
                        // Generate the wav file for voice mail service
                        speechSynthesizer gen = new speechSynthesizer();
                        gen.synthesize(text, javamail.main.glblConfig.getCurrentMessage());
                        return httpGenerator.successResponse("Message successfully updated to: "+text);
                    } else
                        return httpGenerator.inputInvalidResponse("No new message entered");
                } else
                    return httpGenerator.inputInvalidResponse("No new message entered");
            }
        }
        return httpGenerator.inputInvalidResponse("Unknown error occured");
    }
}
